<?php
/**
 * @Description 正则表达式汇总
 * @Sonkwl Xiong
 * @Date 2021/12/06 10:48:20
 */
class validator{

    /**
     * 常用正则
     */
    static public $regArray=array(
        "fno"=>"/^([A-Z]|[a-z]|\d)(\d{4,17}|(\d{4,17}[A-Z]\d{0,4}))|(^\d{18}$)|(^\d{17}(\d|X|x)$)$/",
        "fmail"=>"/^([a-zA-Z]|[0-9])(\w|\-)+@(mail.foxconn.com)|(foxconn.com)$/",
        "ftel"=>"/(^\d{5,6}$)|(^\d{3}-\d{5,6}$)|(^\d{8,9}$)/",
        "sid"=>"/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/",
        "mobile"=>"/(^\d{11}$)|(^([6|9])\d{7}$)|(^[6]([8|6])\d{5})/",
        "date"=>"/(^\d{4}\/([1-9]|1[1-2])\/([1-9]|[1-2][0-9]|3[0-1])$)|(^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|[1-2][0-9]|3[0-1])$)/",
        "month"=>"/^\d{4}\-(0[1-9]|1[0-2])$/",
        "time"=>"/^(([0-1][0-9])|(2[0-3])):([0-5][0-9])$/",
        "mail"=>"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/",
        "url"=>"/[a-zA-z]+://[^\s]/",
        "tel"=>"/\d{3}-\d{8}|\d{4}-\d{7}/",
        "ip"=>"/\d+\.\d+\.\d+\.\d+/"
    );

    /**
     * 验证队列
     */
    static private $cache=array();

    /**
     * 初始化
     */
    public function __construct(){
        self::$cache=array();
    }

    /**
     * 入队列
     */
    static public function Push($val,$rule,$errMsg){
        $arr=explode(":",$rule);
        $reg=$arr[0];
        if($reg=="len"){
            array_push(self::$cache,array('validator',"len",array($val,$errMsg,$arr[1],$arr[2])));
            return;
        }
        if($reg=="minlen"){
            array_push(self::$cache,array('validator',"minlen",array($val,$errMsg,$arr[1])));
            return;
        }
        if($reg=="maxlen"){
            array_push(self::$cache,array('validator',"maxlen",array($val,$errMsg,$arr[1])));
            return;
        }
        if(isset(self::$regArray[$reg])){
            array_push(self::$cache,array('validator',"reg",array($val,$errMsg,self::$regArray[$reg])));
            return;
        }else{
            array_push(self::$cache,array('validator',"maxlen",array($val,$errMsg.",没有验证",0)));//无正则
        }
    }

    /**
     * 运行验证
     */
    static public function Run(){
        for ($i=0; $i < count(self::$cache); $i++) { 
            $msg=call_user_func_array(array(self::$cache[$i][0],self::$cache[$i][1]),self::$cache[$i][2]);
            if($msg) return $msg;
        }
        self::$cache=array();
    }

   /**
    * 字数范围验证器
    */
    static public function len($val,$errMsg,$min,$max){
        if(strlen($val)<$min || strlen($val)>$max){
            return $errMsg;
        }
    }
    /**
    * 最小字数验证器
    */
    static public function minlen($val,$errMsg,$min){
        if(strlen($val)<$min){
            return $errMsg;
        }
    }
    /**
    * 最大字数验证器
    */
    static public function maxlen($val,$errMsg,$max){
        if(strlen($val)>$max){
            return $errMsg;
        }
    }
    /**
     * 正则验证器
     */
    static public function reg($val,$errMsg,$regex){
        if(!preg_match($regex,$val)){
            return $errMsg;
        }
    }
}
// validator::Push("sonkwl","len:1:10","字数在1-10之间");
// validator::Push("F6987445","fno","工号格式错误");
// validator::Push("F6987445@foxconn.com","fmail","邮箱格式错误");
// validator::Push("F6987445@foxconn.com","fmails","没有验证");
// $errMsg=validator::Run();
// if($errMsg) echo $errMsg;
?>